package cn.ds.assets.module.assets.service.dataQualityRule;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.dataQualityRule.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.dataQualityRule.BaseDataQualityRuleDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.dataQualityRule.BaseDataQualityRuleConvert;
import cn.ds.assets.module.assets.dal.mysql.dataQualityRule.BaseDataQualityRuleMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 数据质量规则 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseDataQualityRuleServiceImpl implements BaseDataQualityRuleService {

    @Resource
    private BaseDataQualityRuleMapper baseDataQualityRuleMapper;

    @Override
    public Long createBaseDataQualityRule(BaseDataQualityRuleCreateReqVO createReqVO) {
        // 插入
        BaseDataQualityRuleDO baseDataQualityRule = BaseDataQualityRuleConvert.INSTANCE.convert(createReqVO);
        baseDataQualityRuleMapper.insert(baseDataQualityRule);
        // 返回
        return baseDataQualityRule.getId();
    }

    @Override
    public void updateBaseDataQualityRule(BaseDataQualityRuleUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseDataQualityRuleExists(updateReqVO.getId());
        // 更新
        BaseDataQualityRuleDO updateObj = BaseDataQualityRuleConvert.INSTANCE.convert(updateReqVO);
        baseDataQualityRuleMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseDataQualityRule(Long id) {
        // 校验是否存在
        validateBaseDataQualityRuleExists(id);
        // 逻辑删除
        baseDataQualityRuleMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseDataQualityRuleExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseDataQualityRuleDO dataDo = baseDataQualityRuleMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseDataQualityRuleDO getBaseDataQualityRule(Long id) {
        return baseDataQualityRuleMapper.selectById(id);
    }

    @Override
    public List<BaseDataQualityRuleDO> getBaseDataQualityRuleList(Collection<Long> ids) {
        return baseDataQualityRuleMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseDataQualityRuleDO> getBaseDataQualityRulePage(BaseDataQualityRulePageReqVO pageReqVO) {
        return baseDataQualityRuleMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseDataQualityRuleDO> getBaseDataQualityRuleList(BaseDataQualityRuleExportReqVO exportReqVO) {
        return baseDataQualityRuleMapper.selectList(exportReqVO);
    }

}
